package com.shanghaionstar.user;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.onstar.cn.more.response.VehicleImgRes;
import com.onstar.cn.weight.CustomImageView;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;

/**
 * Created by Mengzhou Gu on 2016/8/31.
 */
public class ChooseVehicleImgDialog extends Dialog {
    private ViewHolder         holder;
    private DialogSaveListener listener;
    private Context            context;
    private VehicleImgRes      mVehicleImgRes;

    public ChooseVehicleImgDialog(Context context) {
        this(context, R.style.Theme_Dialog_NoTitleBar, null);
    }

    public ChooseVehicleImgDialog(Context context, DialogSaveListener listener) {
        this(context, R.style.Theme_Dialog_NoTitleBar, listener);
    }

    public ChooseVehicleImgDialog(Context context, int theme, DialogSaveListener listener) {
        super(context, theme);
        holder = new ViewHolder();
        this.context = context;
        this.listener = listener;
        View view = View.inflate(context, R.layout.choose_vehicle_dia, null);
        holder.init(view);
        setContentView(view);
        setBottomAnimation();
        setFullScreen();
    }

    public void showProgress() {
        holder.llDialog.setDisplayedChild(1);
        holder.progressView.setVisibility(View.VISIBLE);
    }

    public void dismissProgress() {
        holder.progressView.setVisibility(View.GONE);
        holder.llDialog.setDisplayedChild(0);
    }

    /**
     * 底部弹出动画
     */
    private void setBottomAnimation() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.AnimationDialog);
    }

    /**
     * 横向全屏
     */
    private void setFullScreen() {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = (int) (360*context.getResources().getDisplayMetrics().density);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height);
        holder.llDialog.setLayoutParams(lp);
    }

    public void setDialogOccupListener(DialogSaveListener listener) {
        this.listener = listener;
    }

    public void showFailedTips() {
        holder.failedTv.setVisibility(View.VISIBLE);
    }

    public void dismissFailedTips() {
        holder.failedTv.setVisibility(View.INVISIBLE);
    }

    public void showDia(VehicleImgRes res, ImageLoader loader) {
        if (res == null || loader == null)
            return;
        mVehicleImgRes = res;
        String url = res.getThumbnailURL();
        loader.displayImage(url, holder.car_iv, OnstarApplication.getOptions(true, false,R.drawable.onstar_vehicle_img_loading),new AnimateDisplayListener());
        show();
    }

    public class ViewHolder {
        private ViewAnimator    llDialog;
        private LinearLayout    llDialogContent;
        private CustomImageView car_iv;
        private Button          btnCancel;
        private Button          btnConfirm;
        private TextView        failedTv;
        private ProgressLayout  progressView;

        public void init(View view) {
            llDialog = (ViewAnimator) view.findViewById(R.id.ll_dialog);
            llDialogContent = (LinearLayout) view.findViewById(R.id.dia_content_ll);
            car_iv = (CustomImageView) view.findViewById(R.id.vehicle_img_iv);
            btnCancel = (Button) view.findViewById(R.id.btnCancel);
            btnConfirm = (Button) view.findViewById(R.id.btnConfirm);
            failedTv = (TextView) view.findViewById(R.id.failed_tv);
            progressView =  (ProgressLayout) view.findViewById(R.id.dia_progress);

            btnCancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dismissFailedTips();
                    dismiss();
                }
            });

            btnConfirm.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.btnConfirmClick(mVehicleImgRes);
                    }
                }
            });
        }

    }

    public interface DialogSaveListener {
        void btnConfirmClick(VehicleImgRes res);
    }


    public class AnimateDisplayListener extends SimpleImageLoadingListener {

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                FadeInBitmapDisplayer.animate(imageView, 500);
            }
        }

    }
}
